/**
 * Created by ziyu on 17/3/8.
 */
import React, {Component, PropTypes} from 'react'
import {Button, Card, Col, message, Popconfirm, Spin, Table} from "antd"
import moment from 'moment'
import {fetchPost} from '../../../utils/request'
import Search from '../../common/components/Search'
import SearchStyle from "../../common/less/search.less";

class Index extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      list: [],
      panelParams: [],
      params: {
        startDate: moment().subtract('days', 14),
        endDate: moment()
      },
      page: {
        currentPage: 1,
        pageSize: 1000,
        totalCount: 0
      },
      searchParams: {
        list: [
          {
            name: 'Date',
            type: 'range',
            key: ['startDate', 'endDate'],
            className: 'pr20',
            placeHolder: '',
            values: [moment().subtract('days', 14), moment()]
          },
          {
            name: '',
            type: 'search',
            key: '',
            className: 'pr20',
            value: []
          }
        ],
        api: '',
        loading: false
      }
    }
  }

  componentDidMount() {
    this.getData()
  }

  nextPage(current) {
    this.setState({
      page: {
        ...this.state.page,
        currentPage: current
      }
    }, () => {
      this.getData();
    });
  }

  getData() {
    this.setState({
      loading: true
    })
    fetchPost(`/bizStats/thirdService/callTime?currentPage=${this.state.page.currentPage}&pageSize=${this.state.page.pageSize}`, this.state.params).then((res) => {
      if (res.code === 0) {
        // console.log(res)
        let itemList = res.data.itemList
        if (itemList.length > 0) {
          this.setState({
            list: itemList,
            page: res.page,
            loading: false
          })
        } else {
          this.setState({
            list: [],
            page: res.page,
          })
        }
      } else {
        message.error(res.msg)
        this.setState({
          loading: false
        })
      }
    })
  }

  changeParams(params) {
    this.setState({
      params,
    }, () => {
      this.getData();
    })
  }

  render() {
    const self = this;
    const columns = [
      {
        title: 'Statistics Date',
        dataIndex: 'statisticsDate',
        key: 'statisticsDate'
      },
      {
        title: 'Adahaar',
        dataIndex: 'adahaar',
        key: 'adahaar'
      },
      {
        title: 'Driver License',
        dataIndex: 'driverLicense',
        key: 'driverLicense'
      },
      {
        title: 'Voter ID',
        dataIndex: 'voterId',
        key: 'voterId'
      },
      {
        title: 'Pancard',
        dataIndex: 'pancard',
        key: 'pancard'
      },
      {
        title: 'Cibil',
        dataIndex: 'cibil',
        key: 'cibil'
      },
      {
        title: 'Black List',
        dataIndex: 'blackList',
        key: 'blackList'
      },
      {
        title: 'Operator',
        dataIndex: 'operator',
        key: 'operator'
      },
      {
        title: 'SMS',
        dataIndex: 'sms',
        key: 'sms'
      }
    ]

    const pagination = {
      total: this.state.page.totalCount || 0,
      pageSize: this.state.page.pageSize,
      showSizeChanger: false,
      showQuickJumper: true,
      showTotal(total) {
        return `Total ${total} `;
      },
      onChange(current) {
        self.nextPage(current)
      }
    };

    return (
      <div>
        <div className={SearchStyle.searchClass}>
          <Search searchParams={this.state.searchParams} changeParams={(params) => this.changeParams(params)}/>
        </div>

        <div className="mt20">
          <Spin spinning={this.state.loading} delay={300}>
            <Table pagination={pagination} size='small' rowKey="statisticsDate" columns={columns}
                   dataSource={this.state.list}
                   loading={this.state.loading}
                   bordered
            >
            </Table>
          </Spin>
        </div>
      </div>
    )
  }
}

export default Index
